package cn.webestar.scms.user.controller;

import cn.webestar.scms.commons.Assert;
import cn.webestar.scms.commons.R;
import cn.webestar.scms.commons.SysCode;
import cn.webestar.scms.user.entity.User;
import cn.webestar.scms.user.mapper.UserMapper;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Date;

@Slf4j
@RestController
@Tag(name = "测试接口")
@RequestMapping
public class TestController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/unpapi/test/exception")
    public R<String> testException() throws Exception {
        throw new Exception("exception");
    }

    @GetMapping("/unpapi/test/byZero")
    public R<String> testByZero() {
        int i = 1/0;
        return R.success("byZero");
    }

    @GetMapping("/unpapi/test/assert")
    public R<String> testAssert(){
        Assert.isTrue(false, SysCode.biz(1), "assert false");
        return R.success("testAssert");
    }

    @PostMapping("/unpapi/test/count")
    public R<String> testCount(){
        int count = userMapper.count();
        return R.success(count + "");
    }

    @PostMapping("/unpapi/test/insert")
    public R<User> testInsert(){
        User user = new User();
        user.setName("111");
        user.setUsername(LocalDateTime.now().getSecond()+"x");
        user.setPwd("sdssss");
        user.setRegtime(new Date());
        userMapper.insert(user);
        return R.success(user);
    }

    @PostMapping("/unpapi/test/clear")
    public R<Integer> testClear(){
        int i = userMapper.clear();
        return R.success(i);
    }

}
